package com.project.xy.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.Serial;
import com.project.xsf.entity.MDesignProcedure;
import com.project.xsf.entity.MDesignProcedureDetails;
import com.project.xsf.service.MDesignProcedureDetailsService;
import com.project.xsf.service.MDesignProcedureModuleService;
import com.project.xsf.service.MDesignProcedureService;
import com.project.xy.entity.DFile;
import com.project.xy.entity.DModuleDetails;
import com.project.xy.entity.DesignModule;
import com.project.xy.service.DFileService;
import com.project.xy.service.DModuleDetailsService;
import com.project.xy.service.DesignModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@Transactional
@Controller
@RequestMapping("/design")
public class DesignController {
    @Autowired
    private DesignModuleService designModuleService;
    @Autowired
    private DModuleDetailsService dModuleDetailsService;
    @Autowired
    private DFileService dFileService;
    @Autowired
    private MDesignProcedureService mDesignProcedureService;
    @Autowired
    private MDesignProcedureModuleService mDesignProcedureModuleService;
 @Autowired
    private MDesignProcedureDetailsService mDesignProcedureDetailsService;


    @PostMapping("/insertMaterial.action")
    @ResponseBody
    public Integer insertMaterial(DesignModule module , String details) throws IOException {

        String productId = module.getProductId();
        DFile dFile = dFileService.selectByPID(productId);
        int i = designModuleService.insertSelective(module, details,dFile);
        if (i>0){
            dFile.setDESIGN_MODULE_TAG("W001-1");
            dFileService.updateByPrimaryKeySelective(dFile);
        }
        return 1;
    }

    @PostMapping("/selectByComposite.action")
    @ResponseBody
    public List<DesignModule> selectByComposite(String state){
        List<DesignModule> designModules = designModuleService.selectByComposite(state);
        return designModules;
    }

    @GetMapping("/selectById")
    @ResponseBody
    public DesignModule selectById(Integer id ){
        DesignModule designModule = designModuleService.selectByPrimaryKey(id);
        return designModule;
    }

    @PostMapping("/selectDetail.action")
    @ResponseBody
    public List<DModuleDetails> selectDetail(Integer id){

        List<DModuleDetails> details = dModuleDetailsService.selectByParentId(id);
        return details;

    }

    @PostMapping("/materielComposite.action")
    @ResponseBody
    public Integer materielComposite(String design , String details,Integer check){
        DesignModule module = JSONObject.parseObject(design, DesignModule.class);
        int i = 0 ;
        int r = 0;
        if(check==0){
//            审核通过
            module.setCheckTag("S001-1");
            i = designModuleService.updateByPrimaryKeySelective(module);
            DModuleDetails[] moduleDetails = JSONObject.parseObject(details, DModuleDetails[].class);
            for (int j=0; j<moduleDetails.length; j++){
                dModuleDetailsService.updateByPrimaryKeySelective(moduleDetails[j]);
            }
        }else if(check==1){
            module.setCheckTag("S001-2");
            DFile dFile = dFileService.selectByPID(module.getProductId());
            dFile.setDESIGN_MODULE_TAG("W001-0");
            dFileService.updateByPrimaryKeySelective(dFile);
            r = designModuleService.updateByPrimaryKeySelective(module);
//            不通过的designmodule表和的moduledetail表中的数据都被删除
            dModuleDetailsService.deleteByPid(module.getId());
            designModuleService.deleteByPrimaryKey(module.getId());
        }

        if (i==1||r==1){
            return 1;
        }else{
            return 0;
        }

    }

    @PostMapping("/selectDesign.action")
    @ResponseBody
    public List<DesignModule> selectDesign(){


        List<DesignModule> modules = designModuleService.selectList();

        return modules;
    }

    @PostMapping("/selectUpdMateriel.action")
    @ResponseBody
    public List<DesignModule> selectUpdMateriel(){
        List<DesignModule> details = designModuleService.selectUpdMateriel();
        return details;
    }

    @PostMapping("/updMateriel.action")
    @ResponseBody
    public Integer updMateriel(String design, String details) throws IOException {
        DesignModule module = JSONObject.parseObject(design, DesignModule.class);

        module.setCheckTag("S001-0");
//        修改designmodule
        int i2 = designModuleService.updateByPrimaryKeySelective(module);

        dModuleDetailsService.deleteByPid(module.getId());

        DModuleDetails[] moduleDetails = JSONObject.parseObject(details, DModuleDetails[].class);
        for (int i=0;i<moduleDetails.length;i++){
                moduleDetails[i].setParentId(module.getId());
                moduleDetails[i].setDetallsNumber(i+1);
                dModuleDetailsService.insertSelective(moduleDetails[i]);

        }
        //修改物料组成信息表
        MDesignProcedure mDesignProcedure = mDesignProcedureService.queryByProductId(module.getProductId());
        mDesignProcedure.setDesignModuleTag("G002-0");
        mDesignProcedure.setModuleCheckTag("");
        mDesignProcedure.setModuleCostPriceSum(0.0);
        mDesignProcedureService.updateByPrimaryKeySelective(mDesignProcedure);
        //删除物料明细表
         mDesignProcedureModuleService.deleteByMDesignProcedureId(mDesignProcedure.getId());
        List<MDesignProcedureDetails> mDesignProcedureDetails = mDesignProcedureDetailsService.queryMDesignProcedureDetailsByPId(mDesignProcedure.getId());
        for(int i=0;i<mDesignProcedureDetails.size();i++){
            MDesignProcedureDetails mDesignProcedureDetail = mDesignProcedureDetails.get(i);
            mDesignProcedureDetail.setDesignModuleTag("D002-0");
            mDesignProcedureDetail.setDesignModuleChangeTag("D003-0");
            mDesignProcedureDetail.setModuleSubtotal(0.0);
            mDesignProcedureDetailsService.updateByPrimaryKeySelective(mDesignProcedureDetail);
        }
        return i2>0?1:0;
    }
}





































